import time

from selenium import webdriver
from lxml import etree
from time import sleep

from selenium.webdriver.common.by import By

# driver = webdriver.Chrome(r"C:\Users\42082\Desktop\chromedriver-win64\chromedriver.exe")
driver = webdriver.Chrome()
driver.get('https://www.buildhr.com/so/kw%E5%9B%9B%E5%B7%9D.html')
# driver.switch_to_alert().accept()

def get_page(page_text=None):
    if page_text is None:
        page_text = driver.page_source
        # h1 = driver.current_window_handle
        # print(h1)
    #获取新数据示例
    tree = etree.HTML(page_text)
    li_list = tree.xpath('//*[@id="buildhr"]/div[8]/div[1]/div[1]/div[1]/div/div[1]//a/text()')#每页岗位
    print(len(li_list))

    for i in range(len(li_list)):
        if (i % 2 != 0):

            print(li_list[i].strip())
            '''
            D:\03env\Python37\python.exe E:\02project\Pycharm220303\Spider\a209selenium\sele03nextPage.py 
            40
            厂长（污泥处理厂）
            结构设计师
            电气设计师
            给排水设计师
            暖通工程师
            '''
            # pass 下面这4行代码作废，始终会多一行空白
            # print(type(li))#<class 'lxml.etree._ElementUnicodeResult'>

            # name = li.xpath('./dl/@title')[0]
            # print(li.split("\n"))#打印每个岗位

            # print(li.strip())#打印每个岗位
    print("进入下一页")

    # 获取页面高度
    scroll_height = driver.execute_script('return document.documentElement.scrollHeight;')
    print(scroll_height)

    # 获取当前滚动条位置
    scroll_top = driver.execute_script(
        'return document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset;')
    print(scroll_top)

    # 判断是否需要继续滚动
    # if scroll_top < scroll_height:
    #     print("需要继续滚动")
    #     # 需要继续滚动
    #     driver.execute_script('window.scrollTo(0, 500);')
    #     pass
    # else:
    #     # 不需要继续滚动
    #     pass
    # 循环滚动直到页面底部
    while scroll_top < scroll_height:
        # 设置滚动条位置为页面底部
        driver.execute_script('window.scrollTo(0, document.documentElement.scrollHeight);')

        # 等待页面加载
        time.sleep(1)

        # 获取当前滚动条位置
        scroll_top = driver.execute_script(
            'return document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset;')

        driver.find_element(By.XPATH,'//*[@id="buildhr"]/div[8]/div[1]/div[1]/div[2]/div[2]/div/a[12]').click()
        print("已经点击下一页")
        # nextpage=driver.find_element('//*[@id="buildhr"]/div[8]/div[1]/div[1]/div[2]/div[2]/div/a[12]').click()
        # print(nextpage)
        sleep(5)
        # driver.switch_to.window(driver.window_handles[0])
        # h2 = driver.current_window_handle
        # print(h2)
        page_text_ = driver.page_source
        if page_text_ == page_text:
            quit()
        else:
            # 递归
            get_page(page_text_)

if __name__ == '__main__':
    get_page()
    sleep(5)
    driver.quit()
